package com.fourcitizens.playas

import org.codehaus.groovy.grails.commons.ConfigurationHolder;

import com.sun.corba.se.spi.monitoring.StatisticMonitoredAttribute;

import groovyx.net.http.*

class BeachGeocodingService {

    static transactional = true

	List geocodeBeachCloudMade(String countryName, String provinceName, String townName, String beachName) {

		def apiKey = '8ee2a50541944fb9bcedded5165f09d9'
		def baseURL = "http://geocoding.cloudmade.com/"
		def thePath = "$apiKey/geocoding/v2/find.js"
		def theQuery = "Playa+$beachName,$townName,$provinceName,$countryName"
		def http = new HTTPBuilder(baseURL)
		def coordinates = [:]

		http.get( path: thePath,
				  query: [query:theQuery, return_location:true]) { resp, json ->

			println resp.status
			println json
			
			json.each {
				// iterate over JSON 'status' object in the response:
				println it
				
			}
			println json.features
			println json.crs.type
			println json['features']
			coordinates =  json.features.centroid.coordinates
			println "Coordinates: " + coordinates
		}
				  
		return coordinates
    }
	
	List geocodeBeachGoogle(String countryName, String provinceName, String townName, String beachName) {

		def baseURL = "http://maps.google.com"
		def thePath = "maps/api/geocode/json"
		def theQuery = "Playa+$beachName,$townName,$provinceName,$countryName"
		println baseURL + thePath + theQuery
		def http = new HTTPBuilder(baseURL)
		def coordinates = [:]
		
		http.get( path: thePath,
				  query: [address:theQuery, sensor:false]) { resp, json ->
		
			println resp.status
			println json	
		
			if (resp.status.toString() == "200")
			{
				coordinates = json.results.geometry.location
			}
		}
				
		println coordinates
		return coordinates
	}
}
